/*
*   Grupo 2:		Ana Elisa Siena			7126563
*                       Larissa Zimmermann		7126879
* 
*   Jogo em Java - AngryBirds
*   SCC - 604 - Programação Orientada a Objetos
*   Prof. Fernando V. Paulovich
*   Engenharia de Computação 010 - USP - São Carlos
*   Segundo Semestre de 2011 
*	
*/

package angrybirds.personagens;

import angrybirds.colisao.Colisao;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Input;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Vector2f;

public class Passaro extends Colisao {

    private Shape shape;
    private float points[] = new float[12];
    private int click = 0;
    private Vector2f positionMouse;
    private int Vida_passaro = 0;//0 para morto, 1 para vivo

    public void setClick(int click) {
        this.click = click;
    }

    public int getVida_passaro() {
        return Vida_passaro;
    }

    public void setVida_passaro(int Vida_passaro) {
        this.Vida_passaro = Vida_passaro;
    }

    public Passaro(String name, Animation anime, SpriteSheet image, Vector2f position, Vector2f initialDirection,
            Shape collisionShape, int collisionType) {
        super(name, anime, image, position, collisionShape, collisionType);
        this.shape = collisionShape;
    }

    public Shape getShape() {
        return shape;
    }

    public void fly(Input input) {
        Vector2f position = new Vector2f(super.getPosition());//posição do pássaro
        Vector2f finalPosic = new Vector2f();//posição final do pássaro
        float deltaX;
        float deltaY;
        float m;

        if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {

            positionMouse = new Vector2f(input.getAbsoluteMouseX(), input.getAbsoluteMouseY());

            //pega a posição do mouse acima quando ocorre o click.
            this.Vida_passaro = 1;
            this.click = 1;
        }

        if (this.click == 1 && Vida_passaro == 1) {
            if ((super.getPositionX() != positionMouse.getX() || super.getPositionY() != positionMouse.getY())) {
                deltaX = positionMouse.getX() - super.getPositionX();//delta = X1 - X0;
                deltaY = positionMouse.getY() - super.getPositionY();//delta = Y1 - Y0;

                m = deltaY / deltaX;

                float modulo = (float) Math.sqrt(Math.pow(deltaX, 2d) + Math.pow(deltaY, 2d));
                float Xnew = super.getPositionX() + 1.4f * (deltaX) / (modulo);
                float Ynew = super.getPositionY() + 1.4f * (deltaY) / (modulo);
                finalPosic.set(Xnew, Ynew);

                position.set(finalPosic.getX(), finalPosic.getY());
                super.setPosition(position);
                this.shape.setLocation(this.getPositionX(), this.getPositionY());//***importante!!!

                if ((finalPosic.getX() - 1 <= positionMouse.getX() && finalPosic.getX() + 1 >= positionMouse.getX())) {
                    if ((finalPosic.getY() - 1 <= positionMouse.getY() && finalPosic.getY() + 1 >= positionMouse.getY())) {
                        this.Vida_passaro = 0;
                        this.click = 0;
                    }
                }
            }
        }
    }

    public boolean isCollidingWith(Porco collidable) {
        return this.getShape().intersects(collidable.getShape());
    }

    public boolean colidiu(Porco[] anima_Porco) {
        for (int i = 0; i < anima_Porco.length; i++) {
            if (this.isCollidingWith(anima_Porco[i])) {
                return true;
            }
        }
        return false;
    }
}
